package com.mycat.chat.two.ui;

import com.mycat.chat.two.util.Constants;
import com.mycat.chat.two.util.PictureUtil;
import io.netty.util.internal.StringUtil;

import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;
import java.awt.event.*;

/**
 * 窗口面板
 *
 * @author roger.
 * @email 190642964@qq.com
 * @create 2017-02-23
 */
public class ChatRoomPanel extends JPanel {
    /**
     * 好友信息面板
     */
    private JPanel friendInfoPane;
    /**
     * 好友头像
     */
    private JLabel picture;
    /**
     * 好友昵称
     */
    private JLabel nickName;
    /**
     * 好友签名
     */
    private JLabel descript;
    /**
     * 历史消息面板
     */
    private JPanel history;
    /**
     * 历史消息滚动条
     */
    private JScrollPane historyScroll;
    /**
     * 历史消息区域
     */
    public JTextPane historyTextPane;
    /**
     * 工具面板
     */
    private JPanel tools;
    /**
     * 截屏按钮
     */
    private JLabel screen;
    /**
     * 抖动按钮
     */
    private JLabel shake;
    /**
     * 表情按钮
     */
    private JLabel emoticon;
    /**
     * 字体按钮
     */
    private JLabel textFont;
    /**
     * 输入消息面板
     */
    private JPanel input;
    /**
     * 输入消息滚动条
     */
    private JScrollPane inputScroll;
    /**
     * 输入消息区域
     */
    private JTextPane inputTextPane;
    /**
     * 取消按钮
     */
    private JButton quitButton;
    /**
     * 发送按钮
     */
    private JLabel sendButton;

    private JPanel fontPane;
    private JComboBox fontName = null;// 字体名称
    private JComboBox fontSize = null;// 字号大小
    private JComboBox fontStyle = null;// 文字样式
    private JComboBox fontForeColor = null;// 文字颜色
    private JComboBox fontBackColor = null;// 文字背景颜色
    private String[] str_name = {"宋体", "黑体", "Dialog", "Gulim"};
    private String[] str_Size = {"15", "17", "19", "21", "23"};
    private String[] str_Style = {"常规", "斜体", "粗体", "粗斜体"};
    private String[] str_Color = {"黑色", "橙色", "黄色", "绿色"};
    private String[] str_BackColor = {"白色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿"};

    private boolean isFonting;// 是否正在编辑字体

    /**
     * 记录图片
     */
    private StringBuffer imgBuffer = new StringBuffer();
    public Emoticon image;
    private String msg;
    public int position;

    public ChatRoomPanel() {
        initGUI();
        initListener();
    }

    private void initGUI() {
        try {
            setLayout(null);
            setOpaque(false);
            setSize(500, 445);

            // 好友信息面板
            friendInfoPane = new JPanel();
            add(friendInfoPane);
            friendInfoPane.setBounds(0, 0, 635, 70);
            friendInfoPane.setLayout(null);
            friendInfoPane.setOpaque(false);

            picture = new JLabel();
            friendInfoPane.add(picture);
            picture.setBounds(3, 10, 50, 50);
            picture.setBorder(Constants.LIGHT_GRAY_BORDER);
            picture.setIcon(new ImageIcon(PictureUtil.getPicture("/chat/wolffy.jpg")
                    .getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT)));

            nickName = new JLabel();
            friendInfoPane.add(nickName);
            nickName.setFont(Constants.BASIC_FONT);
            nickName.setText("RealName");
            nickName.setBounds(70, 10, 402, 25);

            descript = new JLabel();
            friendInfoPane.add(descript);
            descript.setFont(Constants.BASIC_FONT);
            descript.setBounds(70, 35, 402, 25);
            descript.setText("天天开心");

            // 历史记录
            history = new JPanel();
            add(history);
            history.setBounds(3, 70, 650, 240);
            history.setOpaque(false);
            history.setLayout(new BorderLayout());

            historyScroll = new JScrollPane();
            history.add(historyScroll, BorderLayout.CENTER);
            historyTextPane = new JTextPane();
            historyTextPane.setEditable(false);//不允许编辑
            // 坑爹啊，这么好的属性，jtextpane中竟然在jdk1.6以上不提供，真是太不爽了
//			historyPane.setLineWrap(true);//激活自动换行功能
//			historyPane.setWrapStyleWord(true);//激活断行不断字功能
            historyScroll.setViewportView(historyTextPane);
            historyScroll.getVerticalScrollBar().setUI(new BasicScrollBarUI());
            historyScroll.setBorder(Constants.LIGHT_GRAY_BORDER);

            // 工具栏
            tools = new JPanel();
            add(tools);
            tools.setLayout(null);
            tools.setOpaque(false);
            tools.setBounds(0, 312, 650, 27);

            textFont = new JLabel();
            tools.add(textFont);
            textFont.setBounds(10, 3, 20, 20);
            textFont.setToolTipText("字体");
            textFont.setIcon(PictureUtil.getPicture("/chat/text.png"));

            emoticon = new JLabel();
            tools.add(emoticon);
            emoticon.setBounds(40, 3, 20, 20);
            emoticon.setToolTipText("表情");
            emoticon.setIcon(PictureUtil.getPicture("/chat/emoticon.png"));

            shake = new JLabel();
            tools.add(shake);
            shake.setBounds(70, 3, 20, 20);
            shake.setToolTipText("抖动");
            shake.setIcon(PictureUtil.getPicture("/chat/shake.png"));

            screen = new JLabel();
            tools.add(screen);
            screen.setBounds(100, 3, 20, 20);
            screen.setToolTipText("截屏");
            screen.setIcon(PictureUtil.getPicture("/chat/screenCapture.png"));

            // 输入框
            input = new JPanel();
            add(input);
            input.setBounds(3, 340, 650, 100);
            input.setLayout(new BorderLayout());

            inputScroll = new JScrollPane();
            input.add(inputScroll);
            inputTextPane = new JTextPane();
//			inputTextPane.setTabSize(2);//TAB键多少位
//			inputTextPane.setLineWrap(true);//激活自动换行功能
//			inputTextPane.setWrapStyleWord(true);//激活断行不断字功能
            inputScroll.setViewportView(inputTextPane);
            inputScroll.getVerticalScrollBar().setUI(new BasicScrollBarUI());
            inputScroll.setBorder(Constants.LIGHT_GRAY_BORDER);
            inputScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

            // 取消按钮（关闭）
            quitButton = new JButton();
            add(quitButton);
            quitButton.setBounds(460, 446, 93, 30);
            quitButton.setBorder(Constants.LIGHT_GRAY_BORDER);
            quitButton.setIcon(PictureUtil.getPicture("/chat/quitButton.png"));

            // 发送按钮
            sendButton = new JLabel();
            add(sendButton);
            sendButton.setBounds(560, 446, 93, 30);
            sendButton.setBorder(Constants.LIGHT_GRAY_BORDER);
            sendButton.setIcon(PictureUtil.getPicture("/chat/sendButton.png"));

            // 编辑字体（只做了几个示例）
            fontPane = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
                    try { // 使用Windows的界面风格
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            add(fontPane);
            fontPane.setBounds(3, 280, 650, 25);
            fontPane.setBorder(Constants.LIGHT_GRAY_BORDER);
            fontPane.setLayout(new BoxLayout(fontPane, BoxLayout.X_AXIS));

            fontName = new JComboBox(str_name); // 字体名称
            fontName.setFont(Constants.BASIC_FONT);
            fontSize = new JComboBox(str_Size); // 字号
            fontSize.setFont(Constants.BASIC_FONT);
            fontStyle = new JComboBox(str_Style); // 样式
            fontStyle.setFont(Constants.BASIC_FONT);
            fontForeColor = new JComboBox(str_Color); // 颜色
            fontForeColor.setFont(Constants.BASIC_FONT);
            fontBackColor = new JComboBox(str_BackColor); // 背景颜色
            fontBackColor.setFont(Constants.BASIC_FONT);

            // 开始将所需组件加入容器
            JLabel jlabel_1 = new JLabel("字体：");
            jlabel_1.setFont(Constants.BASIC_FONT);
            JLabel jlabel_2 = new JLabel("样式：");
            jlabel_2.setFont(Constants.BASIC_FONT);
            JLabel jlabel_3 = new JLabel("字号：");
            jlabel_3.setFont(Constants.BASIC_FONT);
            JLabel jlabel_4 = new JLabel("颜色：");
            jlabel_4.setFont(Constants.BASIC_FONT);
            JLabel jlabel_5 = new JLabel("背景：");
            jlabel_5.setFont(Constants.BASIC_FONT);
            fontPane.add(jlabel_1); // 加入标签
            fontPane.add(fontName); // 加入组件
            fontPane.add(jlabel_2);
            fontPane.add(fontStyle);
            fontPane.add(jlabel_3);
            fontPane.add(fontSize);
            fontPane.add(jlabel_4);
            fontPane.add(fontForeColor);
            fontPane.add(jlabel_5);
            fontPane.add(fontBackColor);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    private void initListener() {
        // 工具栏-字体
        textFont.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                if (!isFonting) {
                    textFont.setBorder(BorderFactory.createEmptyBorder());
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                textFont.setBorder(Constants.LIGHT_GRAY_BORDER);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                //3, 70, 650, 240
                if (!isFonting) {
                    isFonting = true;
                    history.setBounds(3, 70, 650, 210);
                    textFont.setBorder(Constants.LIGHT_GRAY_BORDER);
                } else {
                    isFonting = false;
                    textFont.setBorder(BorderFactory.createEmptyBorder());
                    history.setBounds(3, 70, 650, 240);
                }
            }
        });
        // 工具栏-表情
        emoticon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                emoticon.setBorder(BorderFactory.createEmptyBorder());
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                emoticon.setBorder(Constants.LIGHT_GRAY_BORDER);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (null == image) {
                    image = new Emoticon(ChatRoomPanel.this);
                    image.setVisible(true);
                    // 设置控件相对于父窗体的位置
                    Point loc = getLocationOnScreen();
                    image.setBounds(loc.x + 10, loc.y + 30, 350, 300);
                }
                image.requestFocus();
            }
        });
        // 工具栏-抖动
        shake.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                shake.setBorder(BorderFactory.createEmptyBorder());
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                shake.setBorder(Constants.LIGHT_GRAY_BORDER);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("发送消息");
            }
        });
        // 工具栏-截屏
        screen.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                screen.setBorder(BorderFactory.createEmptyBorder());
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                screen.setBorder(Constants.LIGHT_GRAY_BORDER);
            }
        });
        // 回车发送
        inputTextPane.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                msg = inputTextPane.getText();
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    // 发送消息
                    if (msg == null || "".equals(msg.trim())) {
                        System.out.println("发送内容不能为空，请重新输入！");
                    } else {
//                        sendMsg(true);
                    }
                }
            }
        });
        // 发送按钮事件
        sendButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                // 发送消息
                if (null == inputTextPane.getText() || "".equals(inputTextPane.getText())) {
                    System.out.println("发送内容不能为空，请重新输入！");
                } else {
//                    sendMsg(false);
                    System.out.println("发送消息......");
                }
            }
        });
        // 字体样式的所有事件
        fontName.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
            }
        });
        fontSize.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
            }
        });
        fontStyle.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
            }
        });
        fontForeColor.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
            }
        });
        fontBackColor.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
            }
        });
        // 输入框样式，在获取焦点的时候让其自己选择
        inputTextPane.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                if (null != image) {
                    image.dispose();
                    image = null;
                }
            }
        });
        historyTextPane.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                if (null != image) {
                    image.dispose();
                    image = null;
                }
            }
        });
    }

    public void insertIcon(ImageIcon icon) {
        inputTextPane.insertIcon(icon);
    }
}